//
//  BK_Confirm_Order_Tool.m
//  bestkeep
//
//  Created by TOPTEAM on 2017/2/24.
//  Copyright © 2017年 utouu. All rights reserved.
//

#import "BK_Confirm_Order_Tool.h"
#import "BK_OD_Color.h"
#import "BK_OD_Tool.h"
@implementation BK_Confirm_Order_Tool


/*
    接口名称：	deliver-address_default
    接口用途：	获取用户默认的收货地址
    method：	POST
    接口地址：	deliver-address/default
    输入参数：	无
    返回数据：
    {
        "success": "true/false //成功/失败",
        "msg": "xxx //返回消息",
        "code": "xxx //返回编码",
        "data":
        {
            "deliver_id": "xxx//收货地址ID",
            "deliver_name": "xxx//收货人姓名",
            "deliver_mobile": "xxx//收货人手机号",
            "deliver_address": "xxxx//收货地址（完整地址）",
        }
    }
    备注：

 */
+(void)BK_Deliver_Address_Default:(BK_Pay_Callback)CallBack{
    NSString *const OD_Default_Address_URL      = @"deliver-address/default";//获取用户默认的收货地址
    kOrderLoading;
    [[RequestManager manager] bkDataRequest:RequestMethodPost URLString:kOrderURL(OD_Default_Address_URL) parameters:nil success:^(id  _Nullable responseObject){
        
        BOOL success = [responseObject[@"success"] boolValue];
        if (!success) {
            kOrderDismiss;
            kOrderPromptMsg(responseObject[@"msg"]);
//            return;
            CallBack(NO, nil,nil);
        }else{
            kOrderDismiss;
            CallBack(YES, responseObject,nil);
        }
       
        
    }failure:^(id  _Nullable responseObject, NSError * _Nullable error){
        kOrderDismiss;
        NSString *errorMsg;
        if (error.code == -1001) {
            errorMsg = @"请求超时";
        }else if(error.code == 502){
            errorMsg = @"服务器开小差了，请稍后再试！";
        }else{
            errorMsg = @"请求失败";
        }
        kOrderPromptMsg(errorMsg);
        CallBack(NO, nil,error);
    }];
}


/*
接口名称：	order_confirm
接口用途：	用户根据商品生成订单确认数据
method：	POST
接口地址：	order/confirm
输入参数：
key	参数类型	是否必须	参数解释
deliver_id	string	是	收货地址ID
express_companyid	string	否	快递公司ID
goods_id	string	是	商品ID
amount	string	是	购买数量
goods_reserve_id	string	是	商品库存ID
sendDate	string	是	发货日期 2016-06-06
userPrice	string	是	用户出价 25.52
usedShoppingTicketId	string	是	用户使用优惠券ID
integralGoodsId	string	是	积分兑换商品的ID
thnormaltype	string	是	是否囤货普通预售，1：是，0：否
tickets	json	是	购物券兑换的购物券数据
返回数据：
{
    "data":
    {
        "orders":
        [
         {
             "customs_tax_amount": "0.00",
             "deliver_code": "310000",
             "expressCompanyInfo":
             [
              {
                  "defaultSelected": "1",
                  "expressCompanyId": "4KP5TemOScaq4AyYXIXTnQ",
                  "expressCompanyName": "普通快递",
              }
              ],
             "lastSendDateZh": "囤货普通预售最晚发货日期",
             "thNormalType": "0/1,1:是囤货普通预售，0:不是囤货普通预售",
             "express_companyid": "4KP5TemOScaq4AyYXIXTnQ",
             "integralGoodsId": "xxx//积分兑换商品的ID",
             "global_status": "0",
             "goods_total_amount": "12.00",
             "order_amount_lable_list":
             [
              {
                  "index": 0,
                  "lableName": "商品金额",
                  "lableVaule": "￥12.00",
              },
              {
                  "index": 1,
                  "lableName": "运费",
                  "lableVaule": "￥10.00",
              },
              {
                  "index": 2,
                  "lableName": "优惠",
                  "lableVaule": "￥0.00",
              },
              {
                  "index": 3,
                  "lableName": "优惠券",
                  "lableVaule": "￥12.00",
              },
              {
                  "index": 4,
                  "lableName": "合计",
                  "lableVaule": "￥22.00",
              }
              ],
             "order_deliver_address": "上海市",
             "order_express_amount": "10.00",
             "order_items":
             [
              {
                  "goods_amount": "1",
                  "goods_id": "0UevxWimQYu4vPZ08Ec5EA",
                  "goods_img": "http://bestkeep-test.oss-cn-hangzhou.aliyuncs.com/goods/img/goods_1469499624559.jpg@60p_80q",
                  "goods_name": "asdfasd",
                  "canReturnSaled": "是否支持7天无条件可退换货(默认为0：不支持，1支持)",
                  "goods_reserve_id": "QdtyvuzAQsizisJovKOuWQ",
                  "goods_send_date": "",
                  "member_price": "12.00",
                  "member_price_detail":
                  [
                  ],
                  "sale_property": "颜色:red|性别:n",
                  "userPrice": "",
                  "stockShareCode": "xxx//囤货分享码",
              }
              ],
             "order_total_amount": "22.00",
             "promotion_discount_amount": "0.00",
             "shoppingTicketDiscountPrice": "12.00",
             "shoppingTicketList":
             [
              {
                  "invalidTimeStr": "2016-10-21到期",
                  "remainingWarning": "",
                  "selected": "1",
                  "useTypeStr": "仅限国内商品使用",
                  "usedRangeStr": "",
                  "userCouponId": "PkLwcNShRtafx8guvctX8Q",
                  "canUseFlag": "0/1//0:不可使用，1:可使用",
              },
              {
                  "invalidTimeStr": "2016-10-20到期",
                  "remainingWarning": "",
                  "selected": "0",
                  "useTypeStr": "仅限国内商品使用",
                  "usedRangeStr": "",
                  "userCouponId": "W2D6hFajTA-EofELwkgh-Q",
                  "canUseFlag": "0/1//0:不可使用，1:可使用",
              },
              {
                  "invalidTimeStr": "2016-09-30到期",
                  "remainingWarning": "仅剩1天",
                  "selected": "0",
                  "useTypeStr": "仅限国内商品使用",
                  "usedRangeStr": "",
                  "userCouponId": "K_2__G0nRCy4Es-AXYIF7w",
                  "canUseFlag": "0/1//0:不可使用，1:可使用",
              }
              ]
         }
         ],
        "orders_amount": "10.00",
        "orders_express_amount": "10.00",
        "orders_goods_amount": "1",
        "orders_promotion_discount": "0.00",
    },
    "msg": "生成订单信息成功!",
    "success": true,
}
备注：
新添加tickets:[{"couponId":"xxx"}]

*/
+(void)BK_Order_Confirm:(BK_Pay_Callback)CallBack WithParamJsonStr:(NSString *)paramJsonStr{
    
    NSString *const OD_Goods_Confirm_DataURL    = @"order/confirm";//用户根据商品生成订单确认数据
    
    kOrderLoading;
    [[RequestManager manager] bkDataRequest:RequestMethodPost URLString:kOrderURL(OD_Goods_Confirm_DataURL) parameters:@{@"data" : paramJsonStr}
                                    success:^(id  _Nullable responseObject){
                                        BOOL success = [responseObject[@"success"] boolValue];
                                        if (!success){
                                            kOrderDismiss;
                                            kOrderPromptMsg(responseObject[@"msg"]);
                                            CallBack(NO, responseObject,nil);

                                        }else{
                                            kOrderDismiss;
                                            CallBack(YES, responseObject,nil);
                                        }

                                        
                                    }failure:^(id  _Nullable responseObject, NSError * _Nullable error){
                                        kOrderDismiss;
                                        NSString *errorMsg;
                                        if (error.code == -1001) {
                                            errorMsg = @"请求超时";
                                        }else if(error.code == 502){
                                            errorMsg = @"服务器开小差了，请稍后再试！";
                                        }else{
                                            errorMsg = @"请求失败";
                                        }
                                        kOrderPromptMsg(errorMsg);
                                    }];

    
}

/*
    接口名称：	order_submit
    接口用途：	用户提交订单商品信息
    method：	POST
    接口地址：	order/submit
    输入参数：
    key	参数类型	是否必须	参数解释
    source_code	int	是	订单来源1：PC（默认）2：安卓3：IOS
    deliver_id	string	是	收货地址ID
    goods_id	string	是	商品ID
    amount	int	是	购买数量
    goods_reserve_id	string	是	库存id
    sendDate	string	是	发货日期2016-06-06
    userPrice	float	是	用户出价25.52
    remark	string	是	订单备注
    deliver_address	int	是	发货地址code
    express_companyid	string	是	快递公司id
    usedShoppingTicketId	string	是	用户已选优惠券ID
    integralGoodsId	string	是	积分换商品ID
    thnormaltype	string	是	是否囤货普通预售，1：是，0：否
    返回数据：
    {
        "success": "true/false //成功/失败",
        "msg": "xxx //返回消息",
        "code": "xxx //返回编码",
        "data":
        {
            "order_list": "[xxx,xxx]//订单号列表",
        }
    }
    备注：
    请求参数： data={ source_code:xxx//订单来源1：PC（默认）2：安卓3：IOS deliver_id:xxx//收货地址ID orders_info:[{ goods_info:[{ goods_id:xxx//商品ID amount:xxx//购买数量 goods_reserve_id:xxx//库存id sendDate:xxx//发货日期2016-06-06 userPrice:xxx//用户出价25.52 stockShareCode:xxx//囤货分享码 thnormaltype:xxx//是否囤货普通预购，1：是，0：否 }…] remark:xxx//订单备注 deliver_address:xxx//发货地址code express_companyid:xxxxx//快递公司id thnormaltype:xxx//是否囤货普通预购，1：是，0：否 }… ] }
 
 */


+(void)BK_Order_Submit:(BK_Pay_Callback)CallBack WithParamJsonStr:(NSString *)paramJsonStr{

    NSString *const OD_Submit_Order_URL  = @"order/submit";

    if ([RequestManager reachabilityStatus] == RequestReachabilityStatusUnknown || [RequestManager reachabilityStatus] == RequestReachabilityStatusNotReachable) {
        kOrderPromptMsg(@"请检查您的网络");
        return;
    }
    
    kOrderLoading;
    [[RequestManager manager] bkDataRequest:RequestMethodPost URLString:kOrderURL(OD_Submit_Order_URL) parameters:@{@"data" : paramJsonStr}
                                    success:^(id  _Nullable responseObject){
                                        BOOL success = [responseObject[@"success"] boolValue];
                                        if (success == NO){
                                            NSString *promptMsg = responseObject[@"msg"];
                                            if ([promptMsg isEqualToString:@"收货地址异常，未查询到收货地址"]) {
                                                promptMsg = @"请选择收货地址";
                                            }
                                            kOrderDismiss;
                                            kOrderPromptMsg(promptMsg);
                                            return;
                                        }else{
                                            kOrderDismiss;
                                            CallBack(YES, responseObject,nil);
                                        }
                                        
                                        
                                    }failure:^(id  _Nullable responseObject, NSError * _Nullable error){
                                        kOrderDismiss;
                                        NSString *errorMsg;
                                        if (error.code == -1001) {
                                            errorMsg = @"请求超时";
                                        }else if(error.code == 502){
                                            errorMsg = @"服务器开小差了，请稍后再试！";
                                        }else{
                                            errorMsg = @"请求失败";
                                        }
                                        kOrderPromptMsg(errorMsg);
                                    }];


}




@end

















































